<?php


namespace Part20_IteratorMode\Code1;

/**
 * 具体迭代器
 */
class ConcreteIterator extends Iterator
{

    /**
     * 定义了一个具体聚集对象
     * @var ConcreteAggregate
     */
    private $aggregate;
    private $current = 0;

    /**
     * @param $aggregate
     */
    public function __construct($aggregate)
    {
        $this->aggregate = $aggregate;
    }


    public function first()
    {
        return $this->aggregate->getItem(0);
    }


    public function next()
    {
        $this->current = $this->current + 1;
        $ret = null;
        if ($this->current < $this->aggregate->getCount()) {
            $ret = $this->aggregate->getItem($this->current);
        }
        return $ret;
    }


    public function isDone()
    {
        return $this->current >= $this->aggregate->getCount();
    }

    /**
     * @inheritDoc
     */
    public function currentItem()
    {
        return $this->aggregate->getItem($this->current);
    }
}